package com.zeroup.matrix.utile;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteHelper extends SQLiteOpenHelper {
    // 数据库名
    private static final String DATABASE_NAME = "matrix_media.db";

    // 表名
    public static final String USER_TABLE_NAME = "vedio";
    private static final int DATABASE_VERSION = 1;

    //数据库版本号
    public SQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建两个表格:用户表 和职业表
        db.execSQL("CREATE TABLE IF NOT EXISTS " + USER_TABLE_NAME + "(_id INTEGER PRIMARY KEY AUTOINCREMENT," + " vedio_json TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    public Cursor query(){
        SQLiteDatabase database = getReadableDatabase();
        String sql = "select * from vedio where _id = 1";
        return database.rawQuery(sql,null);
    }

    public void update(String vedio_json){
        SQLiteDatabase database = getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("vedio_json",vedio_json);
        String[] whereArgs = { "1" };
        database.update(USER_TABLE_NAME,values,"_id=?",whereArgs);
    }

    public void insert(String vedio_json){
        SQLiteDatabase database = getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("vedio_json",vedio_json);
        values.put("_id",1);
        String[] whereArgs = { "1" };
        database.insert(USER_TABLE_NAME,null,values);
    }

    public void saveOrUpdate(String vedio_json){
        Cursor cursor = query();
        if(cursor.moveToNext()){
            update(vedio_json);
        }else{
            insert(vedio_json);
        }
    }
}
